package com.shawn.mobliesafe;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shawn.utils.MD5Util;

public class HomeActivity extends AppCompatActivity {

    private GridView gv_home_gridview;
    private SharedPreferences sp;
    private AlertDialog dialog;
    private boolean ishide = false;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        sp = getSharedPreferences("config",MODE_PRIVATE);
        gv_home_gridview = (GridView)findViewById(R.id.gv_home_gridview);
        gv_home_gridview.setAdapter(new Myadapter());
        gv_home_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case 0:
                        if (TextUtils.isEmpty(sp.getString("password", ""))) {
                            showSetPassWordDialog();
                        } else {
                            showEnterPasswordDialog();
                        }
                        break;
                    case 1:
                        intent = new Intent(HomeActivity.this,CallSmsSafeActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        intent = new Intent(HomeActivity.this,SoftMangaerActivity.class);
                        startActivity(intent);
                        break;
                    case 3://进程管理
                        Intent intent3 = new Intent(HomeActivity.this,TaskManagerActivity.class);
                        startActivity(intent3);
                        break;
                    case 7:
                        intent = new Intent(HomeActivity.this,AToolsActivity.class);
                        startActivity(intent);
                        break;
                    case 8:
                        intent = new Intent(HomeActivity.this, SettingActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    private void showEnterPasswordDialog() {
        View view = View.inflate(this, R.layout.dialog_enterpassword, null);
        dialog =new AlertDialog.Builder(this).create();
        dialog.setView(view);
        dialog.show();
        final ImageView iv_enterpassword_hide = (ImageView) view.findViewById(R.id.iv_enterpassword_hide);
        final EditText et_setpassword_password = (EditText) view.findViewById(R.id.et_setpassword_password);
        Button btn_ok  = (Button ) view.findViewById(R.id.btn_ok);
        Button btn_cancle   = (Button ) view.findViewById(R.id.btn_cancle);
        iv_enterpassword_hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!ishide) {
                    //显示密码
                    et_setpassword_password.setInputType(0);
                }else {
                    //隐藏密码
                    et_setpassword_password.setInputType(129);//代码设置输入框输入类型
                }
                ishide = !ishide;
            }
        });

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_setpassword_password.getText().toString().trim();
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                String sp_password = sp.getString("password", "");
                if(MD5Util.passwordMD5(password).equals(sp_password)) {
                    Intent intent = new Intent(HomeActivity.this,LostfindActivity.class);
                    startActivity(intent);
                    dialog.dismiss();
                    Toast.makeText(getApplicationContext(), "密码正确", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //隐藏对话框
                dialog.dismiss();
            }
        });
    }

    private void showSetPassWordDialog() {
        View view = View.inflate(this, R.layout.dialog_setpassword,null);
        dialog =new AlertDialog.Builder(this).create();
        dialog.setView(view);
        dialog.show();
        final EditText et_setpassword_password = (EditText) view.findViewById(R.id.et_setpassword_password);
        final EditText et_setpassword_confrim  = (EditText) view.findViewById(R.id.et_setpassword_confrim );
        Button btn_ok  = (Button ) view.findViewById(R.id.btn_ok);
        Button btn_cancle   = (Button ) view.findViewById(R.id.btn_cancle);


        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_setpassword_password.getText().toString().trim();
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                String confrim_password = et_setpassword_confrim.getText().toString().trim();
                if (password.equals(confrim_password)) {
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("password", MD5Util.passwordMD5(password));
                    editor.commit();
                    dialog.dismiss();
                    Toast.makeText(getApplicationContext(), "密码设置成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn_cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //隐藏对话框
                dialog.dismiss();
            }
        });

    }

    private class Myadapter extends BaseAdapter{

        int[] imageId = { R.mipmap.safe, R.mipmap.callmsgsafe, R.mipmap.app,
                R.mipmap.taskmanager, R.mipmap.netmanager, R.mipmap.trojan,
                R.mipmap.sysoptimize, R.mipmap.atools, R.mipmap.settings };
        String[] names = { "手机防盗", "通讯卫士", "软件管理", "进程管理", "流量统计", "手机杀毒", "缓存清理",
                "高级工具", "设置中心" };
        
        @Override
        public int getCount() {
            return imageId.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null) {
                convertView = View.inflate(HomeActivity.this,R.layout.item_home,null);
            }
            ImageView iv_itemhome_icon = (ImageView) convertView.findViewById(R.id.iv_itemhome_icon);
            TextView tv_itemhome_text = (TextView) convertView.findViewById(R.id.tv_itemhome_text);
            iv_itemhome_icon.setImageResource(imageId[position]);
            tv_itemhome_text.setText(names[position]);
            return convertView;
        }
    }
}
